<?php

namespace App\Http\Controllers;

use App\Http\Helpers\StatusResponse;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Psy\Util\Json;
use Tymon\JWTAuth\JWTAuth;

class AuthController extends Controller
{
    /**
     * @OA\Post (
     *     path="/auth/login",
     *     tags={"Auth"},
     *     summary="登录",
     *     description="登录认证，用于对用户认证，使用JWT方式认证",
     * @OA\RequestBody(
     * @OA\MediaType(
     *     mediaType="application/json",
     *     @OA\Schema (
     *     @OA\Property (property="email", type="string"),
     *     @OA\Property (property="password", type="string")
     * ),
     *     example={"email": "admin@mczhengyi.top", "password": "admin"}
     * )
     * ),
     *     @OA\Response(
     *     response=200,
     *     description="OK",
     *
     * )
     * )
     */
    public function login (Request $request)
    {
        if (!$user = User::query()->where('email', \request('email'))->first()) {
            return $this->failWithArr(StatusResponse::USER_NOT_FOUND);
        }
        if ($user->password != \request('password')) {
            return $this->failWithArr(StatusResponse::USER_PASSWORD_ERROR);
        }
        // 认证用户
        $token = Auth::guard('api')->login($user);
//        $credentials = $request->only('email', 'password');
//        $token = Auth::guard('api')->attempt($credentials);
        if ($token) {
            $user->remember_token = $token;
            $user->save();
            return $this->success([
                'access_token' => $token,
                'token_type' => 'bearer',
                'expires_in' => Auth::guard('api')->factory()->getTTL() * 60
            ]);
        }
        return $this->unknown_err();
    }

    /**
     * 获取当前登录用户的信息
     * @api GET /auth/me
     * @requireLogin
     * @param Request $request
     * @return JsonResponse
     */
    public function me (Request $request) {
        $user = Auth::guard('api')->user();
        return $this->success($user);
    }

    /**
     * 未授权的操作返回
     * @api GET /auth/unauthorized
     * @return JsonResponse
     */
    public function unauthorized () : JsonResponse
    {
        return $this->failWithArr(StatusResponse::PERMISSION_REQUIRE);
    }

    /**
     * 登出操作
     * @api PUT /auth/logout
     * @requireLogin
     * @param Request $request
     * @return JsonResponse
     */
    public function logout (Request $request) : JsonResponse
    {
        Auth::guard('api')->logout();
        return $this->success();
    }

    /**
     * 注册操作
     * @api POST /auth/register
     * @param Request $request
     * @return JsonResponse
     */
    public function register (Request $request) : JsonResponse
    {
        $user = [
            'name' => $request->post('username'),
            'password' => $request->post('password'),
            'email' => $request->post('email')
        ];
        if ($user = User::query()->create($user)) {
            return $this->success($user);
        }
        return $this->unknown_err();
    }
}
